package com.github.dengmin.mysql.exception;

import com.github.dengmin.mysql.model.ApiResponse;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author dengmin
 * @Created 2020/6/19 21:37
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ApiResponse doException(Exception e){
        e.printStackTrace();
        return ApiResponse.error(500, e.getMessage());
    }

    @ExceptionHandler(DefaultException.class)
    public ApiResponse doDefaultException(DefaultException e){
        return ApiResponse.error(500, e.getMessage());
    }

    @ExceptionHandler(BadSqlGrammarException.class)
    public ApiResponse badSQL(BadSqlGrammarException e){
        return ApiResponse.error("卧槽!我写的SQL不可能出问题!! \n" +e.getMessage());
    }

}
